import React, { Component } from 'react'
import {
    Text,
    TouchableOpacity
} from 'react-native';

const theTime = '';
//倒计时广告跳转
export default class Timer extends Component {

    constructor(props) {
        super(props)
        this.state = { t: this.props.t };
    }

    componentDidMount() {
        theTime = setInterval(() => {
            this.setState({ t: this.state.t - 1 })
        }, 1000)
    }

    componentDidUpdate() {
        if (this.state.t == -1) {
            this.props.rollBack();
        }
    }

    componentWillUnmount() {
        //卸载
        clearTimeout(theTime);
    }

    setP() {
        clearTimeout(theTime);
        this.props.rollBack();
    }

    render() {
        return (
            <TouchableOpacity activeOpacity={1} onPress={this.setP.bind(this)}>
                <Text>{this.props.text}({this.state.t})</Text>
            </TouchableOpacity>
        )
    }
}
